package com.songlanyun.modules.concerns.controller.api;

import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.account.service.UserInfoService;
import com.songlanyun.modules.concerns.entity.UserConcern;
import com.songlanyun.modules.concerns.model.ConcernDTO;
import com.songlanyun.modules.concerns.model.ConcernStatisticsVo;
import com.songlanyun.modules.concerns.service.UserConcernService;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.service.SendPrivateMessageMsgService;
import com.songlanyun.modules.rabbitmq.service.SendMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 会员的关注表
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2021-01-12 10:07:49
 */
@RestController
@RequestMapping("api/concerns")
public class UserConcernApiController extends AbstractApiController {
    @Autowired
    private UserConcernService userConcernService;
    @Autowired
    private SendMsgService sendMsgService;
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SendPrivateMessageMsgService sendPrivateMessageMsgService;

    /**
     * 我的关注列表
     */
    @GetMapping("/my-concerns")
    //@RequiresPermissions("concerns:userconcern:list")
    public R myConcernsPage(@RequestParam Map<String, Object> params) {
        params.put("user_id", setUserId(params));
        PageUtils page = userConcernService.myConcernsPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 我的粉丝列表
     */
    @GetMapping("/my-fans")
    //@RequiresPermissions("concerns:userconcern:list")
    public R myFansPage(@RequestParam Map<String, Object> params) {
        params.put("user_id", setUserId(params));
        PageUtils page = userConcernService.myFansPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 关注其它会员
     */
    @PostMapping("/concern")
    //@RequiresPermissions("concerns:userconcern:list")
    public R concern(@RequestBody ConcernDTO dto) {
        UserConcern concern = userConcernService.concern(getUserId(true), dto);

        //发送消息  --给被关注人
        sendPrivateMessageMsgService.send(new MessageTO(PrivateMessageConstant.MessageType.INTERACTION_MESSAGE,
                PrivateMessageConstant.ModelType.CONCERNS,concern.getConcernUserId(),concern.getConcernedUserId()));

        return R.ok().put("data", concern);
    }

    /**
     * 取消关注
     */
    @PostMapping("/cancel-concern")
    //@RequiresPermissions("concerns:userconcern:list")
    public R cancelConcern(@RequestBody ConcernDTO dto) {
        userConcernService.cancelConcern(getUserId(true), dto);
        return R.ok();
    }


    /**
     * 统计我关注的人数与我的粉丝人数
     */
    @GetMapping("/statistics")
    //@RequiresPermissions("concerns:userconcern:list")
    public R statistics() {

        ConcernStatisticsVo vo = userConcernService.statisticsPerson(getUserId(true));
        return R.ok().put("data", vo);
    }


}
